package org.bedracket.pgeg.capability;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.bedracket.pgeg.init.ModCapabilities;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nonnull;

public class ManaLevelCapabilityProvider extends CapabilityProvider<ManaLevelCapabilityProvider> implements ICapabilitySerializable<CompoundTag> {

    private final LazyOptional<ManaLevelCapability> handler;
    @Nonnull
    private final ManaLevelCapability instance;

    public ManaLevelCapabilityProvider() {
        super(ManaLevelCapabilityProvider.class);
        this.handler = LazyOptional.of(this::getInstance);
        instance = new ManaLevelCapability();
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        return ModCapabilities.PLAYER_MANA_LEVEL.orEmpty(cap, handler);
    }

    @Override
    public CompoundTag serializeNBT() {
        return instance.serializeNBT();
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        instance.deserializeNBT(nbt);
    }

    public ManaLevelCapability getInstance() {
        return instance;
    }
}
